from apps.dao.UserRepository import UserRepository


class UserService:
    @staticmethod
    def create_user(username, email):
        return UserRepository.create_user(username, email)

    @staticmethod
    def get_user_by_id(user_id):
        return UserRepository.get_user_by_id(user_id)

    @staticmethod
    def get_all_users():
        userList = UserRepository.get_all_users()
        count = UserRepository.get_all_users_count()
        data = {
            'code': 0,
            'msg': '查询成功',
            'count': count,
            'data': userList
        }
        return data


    @staticmethod
    def update_user(user_id, new_username):
        user = UserRepository.get_user_by_id(user_id)
        if user:
            UserRepository.update_user(user, new_username)
            return user
        return None

    @staticmethod
    def delete_user(user_id):
        user = UserRepository.get_user_by_id(user_id)
        if user:
            UserRepository.delete_user(user)
